package com.ddw.wotcg.server.controllers;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;

import com.ddw.wotcg.server.utils.RequestHelper;
import com.ddw.wotcg.server.utils.TemplateHelper;
import com.ddw.wotcg.server.utils.User;
import com.ddw.wotcg.server.utils.UserHelper;

@SuppressWarnings("serial")
public class LobbyController extends ControllerBase {
	
	public void create() throws IOException, ServletException {
		final Map<String, Object> root = new HashMap<String, Object>();
		final Map<String, String> cookies = RequestHelper
				.parseCookies(this.request);
		final UUID session = RequestHelper.getSession(cookies);
		final User user = UserHelper.bySession(session);
		
		// If user is not logged in then redirect them
		if (user == null) {
			this.response.sendRedirect("/home");
			return;
		}
		
		// Populate defaults
		root.put("gameName", "");
		root.put("gameNameInvalid", null);
		root.put("scenario", "");
		root.put("scenarioInvalid", null);
		root.put("mode", "");
		root.put("modeInvalid", null);
		
		TemplateHelper.appendPageData(root, "Create Game");
		TemplateHelper.appendUserData(root, user);
		
		TemplateHelper.callTemplate(this.config, this.response,
				"/lobby/create.ftl", root);
	}
	
	public void index() throws IOException, ServletException {
		final Map<String, Object> root = new HashMap<String, Object>();
		final Map<String, String> cookies = RequestHelper
				.parseCookies(this.request);
		final UUID session = RequestHelper.getSession(cookies);
		final User user = UserHelper.bySession(session);
		
		// If user is not logged in then redirect them
		if (user == null) {
			this.response.sendRedirect("/home");
			return;
		}
		
		TemplateHelper.appendPageData(root, "Lobby");
		TemplateHelper.appendUserData(root, user);
		
		TemplateHelper.callTemplate(this.config, this.response,
				"/lobby/index.ftl", root);
	}
	
}
